/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.wdb.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author vacoor
 */
public abstract class ResourceServlet extends HttpServlet {
    protected final String resourcePath;

    protected ResourceServlet(String resourcePath) {
        this.resourcePath = resourcePath;
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO 这里应该有个认证
        if (request.getParameter("debug") != null) {
            request.getSession().setAttribute("debug", 1);
        }
        if (request.getSession().getAttribute("debug") == null) {
            response.sendError(404);
            return;
        }

        String contextPath = request.getContextPath();
        String path = request.getPathInfo();

        contextPath = contextPath != null ? contextPath : "";
        if (path == null) {
            response.sendRedirect(contextPath + request.getServletPath() + "/index.html");
            return;
        }

        if ("/".equals(path)) {
            response.sendRedirect("index.html");
            return;
        }

        response.setCharacterEncoding("UTF-8");
        if (path.endsWith(".html")) {
            response.setContentType("text/html");
            writeResource(response, resourcePath + path);
        } else if (path.endsWith(".css")) {
            response.setContentType("text/css");
            writeResource(response, resourcePath + path);
        } else if (path.endsWith(".js")) {
            response.setContentType("text/javascript");
            writeResource(response, resourcePath + path);
        } else if (path.endsWith(".png") || path.endsWith(".jpg") || path.endsWith(".gif") || path.endsWith("ico")) {
            writeResource(response, resourcePath + path);
        } else if (path.endsWith(".swf")) {
            response.setContentType("application/x-shockwave-flash");
            writeResource(response, resourcePath + path);
        } else if (path.endsWith(".woff")) {
            response.setContentType("application/x-font-woff");
            writeResource(response, resourcePath + path);
        } else {
            handleRequest(path, request, response);
        }
    }

    protected void writeResource(HttpServletResponse response, String path) throws IOException {
        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
        if (is == null) {
            response.sendError(404);
            return;
        }

        try {
            int readed;
            byte[] buffer = new byte[1024];
            ServletOutputStream os = response.getOutputStream();

            while (-1 < (readed = is.read(buffer))) {
                os.write(buffer, 0, readed);
            }
            os.flush();
        } finally {
            try {
                is.close();
            } catch (IOException ignore) {
                // ignore
            }
        }
    }

    protected abstract void handleRequest(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
